package com.example.vertification.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.vertification.Utils.RedisUtil;
import com.example.vertification.entity.Order;
import com.example.vertification.entity.Question;
import com.example.vertification.entity.User;
import com.example.vertification.entity.UserStatics;
import com.example.vertification.service.OrderService;
import com.example.vertification.service.QuestionService;
import com.example.vertification.service.UserService;
import com.example.vertification.service.UserStaticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "排名中心", tags = "排名中心")
public class RankController {

    private final UserService userService;
    private final OrderService orderService;
    private  final UserStaticsService userStaticsService;



    @Autowired
    public RankController(UserService userService, OrderService orderService, UserStaticsService userStaticsService) {
        this.userService = userService;
        this.orderService = orderService;
        this.userStaticsService = userStaticsService;
    }



    @GetMapping("/get_rank")
    @ApiOperation(value = "获取各项数据排名")
    public ResponseEntity<?> get_score_rank() throws IOException {
       List<User> users_list = userService.getAll();
        for(User u:users_list){
            u.setPassword("**********");
        }


       JSONArray json_arr= JSONUtil.parseArray(users_list);
        for(int i =0;i<json_arr.size();i++){
            JSONObject obj = (JSONObject) json_arr.get(i);
            UserStatics us_temp = userStaticsService.getEntityByUid(Long.valueOf(obj.getStr("uid")));
            if(us_temp==null){
                obj.set("question_yes",1);
                obj.set("question_no",1);
            }else{
                obj.set("question_yes",us_temp.getQuestionYes());
                obj.set("question_no",us_temp.getQuestionNo());
            }
            List<Order> order_buys_list = orderService.getAllBuysByUserID(Long.valueOf(obj.getStr("uid")));
            int buys_amount =0;
            for(Order o:order_buys_list){
                buys_amount+=o.getProduct_amount();
            }
            obj.set("buy",buys_amount);
            List<Order> order_sell_list = orderService.getAllSellsByUserID(Long.valueOf(obj.getStr("uid")));
            int sell_amount =0;
            for(Order o:order_sell_list){
                sell_amount+=o.getProduct_amount();
            }
            obj.set("sell",buys_amount);

            json_arr.set(i,obj);
        }
        return ResponseEntity.ok(json_arr);
    }





}
